package vng.wcc.sonph.LauncherGenerator;

import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.view.Display;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class Formatter {


	private static final int RATIO_TOP_PADDING_2_NORMAL_PADDING = 3;
	private static final int RATIO_TOP_TAB_HEIGHT_2_NORMAL_PADDING = 8;
	//	private static final double RATIO_HEGHT_WIDTH = 1.333;
	private int screenWidth;
	private int screenHeight;
	private int cellWidth;
	private int cellHeight;
	private int rowHeight;
	private int rowWidth;
	private int cellPadding;


	private int iconImageSize;
	private int iconLayoutWidth;
	private int iconLeftPadding;
	private int iconRightPadding;
	private int iconTopPadding;
	private int iconBottomPadding;
	private int titleWidth;
	private int iconLayoutHeight;
	private int titleHeight;
	private int titleLeftPadding;
	private int titleRightPadding;
	private int titleTopPadding;
	private int titleBottomPadding;
	private int numColsPerPage;

	private int paddingAll;
	private int launcherTopPadding;


	public int getPaddingAll() {
		return paddingAll;
	}

	//System icon tab: 
	// is the tab on the top of screen: (show you how many battery left, how good the signal is, 3G is on/off ... ?
	static HashMap<Integer,Integer> mapScreenWidth2SystemIconsTabHeight = new HashMap<Integer, Integer>();
	static {
		mapScreenWidth2SystemIconsTabHeight.put(240, 24); //38
		mapScreenWidth2SystemIconsTabHeight.put(320, 32); //50
		mapScreenWidth2SystemIconsTabHeight.put(480, 48); //76
	}

	/**
	 * This class provides layout information for all views
	 * @param context
	 * @param numColsPerPage number of columns per row on each page
	 */
	public Formatter(Context context,int numColsPerPage) {

		this.numColsPerPage = numColsPerPage;		
		Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		this.screenWidth = display.getWidth();
		this.screenHeight = display.getHeight();

		//		this.paddingAll = (int)Math.round((1.0*screenWidth)/(10*numColsPerPage-2));
		 
		this.paddingAll = (int)Math.round((1.0*screenWidth)/(20*numColsPerPage-2));
		this.launcherTopPadding = RATIO_TOP_PADDING_2_NORMAL_PADDING * paddingAll;

		this.cellWidth = (screenWidth - 2*paddingAll) / numColsPerPage;		
		this.cellPadding = paddingAll;

		this.iconImageSize = getImageSizeBaseOnPadding();
		this.iconLayoutWidth = cellWidth - cellPadding*2;
		if ( iconImageSize > iconLayoutWidth ) iconImageSize = iconLayoutWidth-2; 
		this.iconTopPadding = 0;
		this.iconLeftPadding = (iconLayoutWidth - iconImageSize)/2;
		this.iconBottomPadding = cellPadding;
		this.iconRightPadding = iconLeftPadding;
		this.iconLayoutHeight = iconTopPadding + iconImageSize + iconBottomPadding;


		this.titleWidth = cellWidth - 2*cellPadding;
		this.titleHeight = getTitleHeightBaseOnScreenWidth();
		this.titleLeftPadding = 0;
		this.titleRightPadding = 0;
		this.titleTopPadding = 0;
		this.titleBottomPadding = 0;

		this.cellHeight = 3*paddingAll + iconImageSize + titleHeight;
		this.rowHeight = cellHeight;
		this.rowWidth = LinearLayout.LayoutParams.FILL_PARENT;
	}




	private int getTitleHeightBaseOnScreenWidth() {
		return (5*(screenWidth*7/160) / 2)+1;		
	}

	public int getNumColsPerPage() {
		return numColsPerPage;
	}

	private int getImageSizeBaseOnPadding() {
		return cellWidth - 4 * paddingAll - screenWidth / 15; //40 = standard
	}

	public int getScreenWidth() {
		return screenWidth;
	}

	public int getScreenHeight() {
		return screenHeight;
	}

	public int getCellWidth() {
		return cellWidth;
	}

	public int getCellHeight() {
		return cellHeight;
	}

	public int getRowHeight() {
		return rowHeight;
	}

	public int getRowWidth() {
		return rowWidth;
	}

	public int getCellPadding() {
		return cellPadding;
	}

	public int getIconImageSize() {
		return iconImageSize;
	}

	public int getIconLayoutWidth() {
		return iconLayoutWidth;
	}

	public int getIconLeftPadding() {
		return iconLeftPadding;
	}

	public int getIconRightPadding() {
		return iconRightPadding;
	}

	public int getIconTopPadding() {
		return iconTopPadding;
	}

	public int getIconBottomPadding() {
		return iconBottomPadding;
	}

	public int getTitleWidth() {
		return titleWidth;
	}

	public int getIconLayoutHeight() {
		return iconLayoutHeight;
	}

	public int getTitleHeight() {
		return titleHeight;
	}

	public int getTitleLeftPadding() {
		return titleLeftPadding;
	}

	public int getTitleRightPadding() {
		return titleRightPadding;
	}

	public int getTitleTopPadding() {
		return titleTopPadding;
	}
	public int getLauncherTopPadding() {
		return launcherTopPadding;
	}
	
	public int getTitleBottomPadding() {
		return titleBottomPadding;
	}
	public int getFlipperViewHeight(TabFormatter tabFormatter) {
		int systemIconsHeight = screenWidth / 10;
		if ( mapScreenWidth2SystemIconsTabHeight.containsKey(screenWidth)) {
			systemIconsHeight = mapScreenWidth2SystemIconsTabHeight.get(screenWidth);
		}
		return screenHeight - systemIconsHeight - this.getTopTabHeight() - tabFormatter.getTabHeight();
		
//		return this.cellHeight * numColsPerPage + launcherTopPadding;

	}

	public static Bitmap scaleImageResource(Context context,int resid,int newWidth,int newHeight) {
		Bitmap bitmapOrg = BitmapFactory.decodeResource(
				context.getResources(), 
				resid);

		int width = bitmapOrg.getWidth();
		int height = bitmapOrg.getHeight();

		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		// create a matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight);
		// recreate the new Bitmap
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
				width, height, matrix, true); 

		return resizedBitmap;
	}




	public int getTopTabHeight() {
		return RATIO_TOP_TAB_HEIGHT_2_NORMAL_PADDING * paddingAll;
	}

}
